﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;


namespace PuzzleCoop
{
    public class Character : GameObject
    {
        #region Atributtes
        public int TotalHP = 10;
        public int TotalMP = 10;
        public int HP = 10;
        public int MP = 10;
        public float velocity = 5;
        #endregion

        public Character(PlayScreen playScreen, Texture2D sprite)
            : base(playScreen, sprite)
        {
        }

        public override void Update(GameTime gameTime)
        {
            // tentativa de colisão entre os GameObjects, mas não funciona (não sei pq)
            //foreach (GameObject go in playScreen.gameObjects) if (go != this && Collision(go)) position = prevPosition;
            if (HP <= 0) { HP = 0; MP = 0; alive = false; return; }
            direction.Normalize();
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            base.Draw(spriteBatch);
        }

        public void Move(Vector2 d)
        {
            if (d == null || d == Vector2.Zero) return;
            d.Normalize();
            direction = d;
            Vector2 prevPos = position;
            Vector2 a = d * velocity;
            position = new Vector2(position.X + a.X, position.Y);
            if (CheckCollisionWithBoard()) position = new Vector2(prevPos.X, position.Y);
            position = new Vector2(position.X, position.Y + a.Y);
            if (CheckCollisionWithBoard()) position = new Vector2(position.X, prevPos.Y);
        }

        #region HP/MP
        public int TakeDamage(int damage)
        {
            if (damage <= 0) return 0;
            if (damage > HP) damage = HP;
            HP -= damage;
            return damage;
        }

        public int GainHP(int amount)
        {
            if (amount <= 0) return 0;
            if (amount + HP > TotalHP) amount = TotalHP - HP;
            HP += amount;
            return amount;
        }

        public int GainMP(int amount)
        {
            if (amount <= 0) return 0;
            if (amount + MP > TotalMP) amount = TotalMP - MP;
            MP += amount;
            return amount;
        }

        public bool ConsumeMana(int manaCost)
        {
            if (manaCost > MP) return false;
            MP -= manaCost;
            return true;
        }
        #endregion
    }
}
